package org.dragonnova.meetingclient.model;

import org.dragonnova.meetingclient.core.net.socket.UdpPacket;

import java.net.Socket;
import java.util.ArrayList;

/**
 * 用于观察WIFI连接的事件派发
 * Created by Administrator on 2015/8/3 17:15.
 * Email: thinkdata@163.com
 */
public abstract class AObservable {

    private final ArrayList<IObserver> mObservers = new ArrayList<IObserver>();

    private Socket clientSocket;

    private UdpPacket udpPacket;

    public abstract void handler(Object... args);

    /**
     * 触发观察对象
     *
     * @param args
     */
    public void notifyObservers(Object... args) {
        int size = 0;
        IObserver[] observers;
        synchronized (this) {
            size = mObservers.size();
            observers = new IObserver[size];
            mObservers.toArray(observers);
        }
        for (IObserver obs : observers) {
            obs.doWhat(this, args);
        }
    }

    /**
     * 添加
     *
     * @param obs
     */
    public void addObserver(IObserver obs) {
        if (obs == null)
            throw new NullPointerException("observer is null");

        synchronized (mObservers) {
            if (!mObservers.contains(obs))
                this.mObservers.add(obs);
        }
    }

    public Socket getClientSocket() {
        return clientSocket;
    }

    public void setClientSocket(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    public UdpPacket getUdpPacket() {
        return udpPacket;
    }

    public void setUdpPacket(UdpPacket udpPacket) {
        this.udpPacket = udpPacket;
    }
}
